package com.rico.deploy.service.impl;

import com.github.pagehelper.PageHelper;
import com.rico.deploy.entity.*;
import com.rico.deploy.mapper.CampusMapper;
import com.rico.deploy.mapper.CollegeMapper;
import com.rico.deploy.mapper.MajorMapper;
import com.rico.deploy.mapper.SubjectMapper;
import com.rico.deploy.service.CollegeService;
import com.rico.deploy.service.SubjectService;
import com.rico.deploy.vo.DtreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 夏智力
 * @FileName SubjectServiceImpl
 * @date 2021/5/2 17:51
 * @Software: IntelliJ IDEA
 */
@Service
public class SubjectServiceImpl implements SubjectService {
    @Autowired
    private SubjectMapper subjectMapper;

    @Autowired
    private CampusMapper campusMapper;

    @Autowired
    private CollegeMapper collegeMapper;

    @Autowired
    private MajorMapper majorMapper;

    @Override
    public List<DtreeVo> LeftTreeView() {
        // 创建插件数据格式
        List<DtreeVo> dtreeVos = new ArrayList<>();
        // 校区
        List<Campus> campuses = campusMapper.selectAll();
        for ( Campus campus : campuses ){
            DtreeVo dtreeVo = new DtreeVo();
            dtreeVo.setId(campus.getCampusCode());  // id
            dtreeVo.setTitle(campus.getCampusName()); // 名称
            dtreeVo.setCheckArr("0");
            dtreeVo.setParentId("0");
            dtreeVos.add(dtreeVo);
        }
        // 学院
        List<College> colleges = collegeMapper.selectAll();
        for ( College college : colleges ){
            DtreeVo dtreeVo = new DtreeVo();
            dtreeVo.setId("0"+college.getId()); // id
            dtreeVo.setTitle(college.getCollegeName()); // 名称
            dtreeVo.setParentId(college.getCampusCode()); // 父类id
            dtreeVo.setCheckArr("0");
            dtreeVos.add(dtreeVo);
        }
        //专业
        List<Major> majors = majorMapper.selectAll();
        for(Major major :majors){
            DtreeVo dtreeVo = new DtreeVo();
            dtreeVo.setId("00"+major.getId()); // id
            dtreeVo.setTitle(major.getMajorName()); // 名称
            Integer id = 0;
            for(College college : colleges){
                if(major.getCollegeCode().equals(college.getCollegeCode()))
                    id = college.getId();
            }
            dtreeVo.setParentId("0"+id); // 父类id
            dtreeVo.setCheckArr("0");
            dtreeVos.add(dtreeVo);
        }
        return dtreeVos;
    }

    @Override
    public List<Subject> selectAll() {
        return subjectMapper.selectAll();
    }

    @Override
    public List<Subject> selectAll(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        return subjectMapper.selectAll();
    }

    @Override
    public List<Subject> selectAll(Integer page, Integer limit, String keyword) {
        Example example = new Example(Subject.class);
        example.createCriteria()
                .andEqualTo("majorName",keyword);
       return subjectMapper.selectByExample(example);
    }

    @Override
    public Subject selectById(Integer id) {
        return subjectMapper.selectByPrimaryKey(id);
    }

    @Override
    public int insertSubject(Subject subject) {
        return subjectMapper.insert(subject);
    }

    @Override
    public int deleteSubjectById(Integer id) {
        return subjectMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateSubject(Subject subject) {
        return subjectMapper.updateByPrimaryKeySelective(subject);
    }
}
